import {ElMessage, ElMessageBox} from "element-plus";
import {isRelogin} from "./request";
import useUserStore from '@/store/modules/user'

const responseError = error => {
    try {
        // status 是 HTTP 状态码
        
        const status = error.response.status
        switch (status) {
            case 400:
                error.message = "请求错误"
                break
            case 401:
                error.message = "" 
                if(!isRelogin.show){
                    useUserStore().logOut().then(() => {
                        location.href = '/index';
                        return Promise.reject()
                    })
                    
                    ElMessage.error("无效的会话，或者会话已过期，请重新登录。")
                    isRelogin.show = true
                }
                break
            case 403:
                error.message = "拒绝访问"
                break
            case 404:
                error.message = "请求地址出错"
                break
            case 408:
                error.message = "请求超时"
                break
            case 500:
                error.message = "服务器内部错误"
                break
            case 501:
                error.message = "服务未实现"
                break
            case 502:
                error.message = "网关错误"
                break
            case 503:
                error.message = "服务不可用"
                break
            case 504:
                error.message = "网关超时"
                break
            case 505:
                error.message = "HTTP 版本不受支持"
                break
            default:
                break
        }
        error.message && ElMessage.error(error.message)
        return Promise.reject(error)
    } catch (e) {}
    return Promise.reject(error)
}

const extendsFn = {
    responseError
}

// 将request的error导出 存window 给子组件共用


export default extendsFn